<template>
  <div>
    <a-modal
      title="物流信息"
      :confirm-loading="sendModalLoading"
      :mask-closable="false"
      :destroy-on-close="true"
      :visible="visible"
      @ok="send"
      @cancel="$emit('update:visible', false)"
    >
      <a-alert
        v-if="showNotive"
        message="注意"
        description="该订单包含门店自提配送，请确认所有商品都已准备完成再发货！"
        type="error"
      />
      <a-tabs :default-active-key="1" v-model="activeKey">
        <a-tab-pane tab="快递" key="1">
          <a-form-item
            >快递公司：
            <a-select style="width: 150px" placeholder="请选择" v-model="express.selectExpress">
              <a-select-option :value="index" v-for="(item, index) of expressList" :key="index">{{
                item
              }}</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item>
            收件人邮编：<a-input v-model="express.post_code" />
            <a-button @click="print()" size="small">电子面单</a-button>
            <div></div>
            <a href='http://www.c-lodop.com/download.html' target='_blank' class='ts'>电子面单打印服务下载地址</a>
          </a-form-item>
          <a-form-item> 快递单号：<a-input v-model="express.express_no" /> </a-form-item>
          <a-form-item>商家留言（选填）：<a-textarea v-model="express.words" /></a-form-item>
        </a-tab-pane>
        <a-tab-pane tab="无需物流" key="0">
          <a-form-item>商家留言（选填）：<a-textarea v-model="express.words" /></a-form-item>
        </a-tab-pane>
      </a-tabs>
    </a-modal>
  </div>
</template>

<script>
import {
  modifyPrice,
  getOrderList,
  orderCancel,
  setComments,
  setAddress,
  orderSend,
  orderConfirm,
  orderPrint,
  orderClerk,
  orderPrintMd,
} from '@/api/order'
import { getLodop } from '@/utils/LodopFuncs.js'
export default {
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    expressList: {
      type: Array,
      default: () => [],
    },
    orderId: {
      type: String,
      default: '',
    },
    orderInfo: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      activeKey: '1',
      express: {
        words: '',
        express_no: ''
      },
      sendModalLoading: false,
      showNotive: false,
    }
  },
  computed: {},
  watch: {
    visible(val) {
      if (val && this.orderInfo?.id) {
        let haveExpress = this.orderInfo.goods_list.some((d) => {
          return d.delivery_type == '0'
        })
        let haveShop = this.orderInfo.goods_list.some((d) => {
          return d.delivery_type == '1'
        })
        // 如果有物流和门店自提订单时，发货时需要提醒该订单包含门店自提
        if (haveExpress && haveShop) {
          this.showNotive = true
        }
      }
    },
  },
  created() {},
  mounted() {},
  updated() {},
  methods: {
    send() {
      const hide = this.$message.loading('发货中...', 0)
      var params = {
        order_id: this.orderId,
        words: this.express.words,
      }
      if (this.activeKey === '1') {
        params.is_express = 1
        params.express = this.expressList[this.express.selectExpress]
        params.post_code = this.express.post_code
        params.express_no = this.express.express_no
      } else {
        params.is_express = 0
      }
      orderSend(params).then((res) => {
        hide()
        if (res.code === 0) {
          this.express = {
            words: '',
            express_no: '',
            selectExpress: '',
          }
          this.$message.success('发货成功！')
          this.$emit('update:visible', false)
          this.$emit('onAfterClose')
        } else {
          this.$error({
            content: res.msg,
          })
        }
      })
    },
    // 打印电子面单
    print() {
      let LODOP= getLodop();
      console.log('LODOP,',LODOP)
      if(!LODOP){
        this.$error({
          content: '请运行或者下载打印插件',
        })
        return
      }
      if (this.expressList[this.express.selectExpress] ) {
        const hide = this.$message.loading('打印中...', 0)
        orderPrintMd({
          id: this.orderId,
          express: this.expressList[this.express.selectExpress],
          code: this.express.post_code,
        }).then((res) => {
          hide()
          if (res.code == 0) {
            this.express.express_no = res.data.Order.LogisticCode
            LODOP.PRINT_INIT("");
            LODOP.ADD_PRINT_HTM(10, 50, '100%', '100%', res.data.PrintTemplate);
            LODOP.PRINT_DESIGN();
            this.$message.success('打印成功');
            return;
          } else {
            this.$error({
              content: res.msg,
            })
          }
        })
      } else {
        this.$error({
          content: '请选择快递',
        })
      }
    },
  },
}
</script>

<style scoped>
</style>